/******************************************************************************
 * * sms_main.c - implementation of sms entry and common functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "sms_include.h"

Msg_Global_Ctrl_t g_MsgGlobalCtrl;
const INT8        g_AcceptInputNum[13] = { '+', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '#', '0' };

/**
 * initiate sms module
 * param (in) void
 * return  void
 */
VOID Message_Init_Req(VOID)
{
    UINT8                   *data    = Hal_Mem_Alloc(3);
    If_Framework_Reg_Info_t *RegInfo = NULL;

    // init global variable
    Hal_Mem_Set(&g_MsgGlobalCtrl, 0, sizeof(Msg_Global_Ctrl_t));

//    MMI_Modem_Get_Smsc_Req(MMI_MODEM_SIM_1);    // temp
    // read NVM
    UI_NV_Read_Req(NV_SECTION_UI_SMS, 0, 1, data);
    g_MsgGlobalCtrl.DictionaryFlg = (MSG_DICTIONARY_ENABLE_TYPE)data[0];

    if (MSG_DICTIONARY_ENB == g_MsgGlobalCtrl.DictionaryFlg)
    {
        IME_Config_Req(MESSAGE_IME_DICTIONARY_ENABLE);
    }
    else
    {
        IME_Config_Req(MESSAGE_IME_DICTIONARY_OFF);
    }
    Hal_Mem_Free(data);

    data = NULL;
    // set the delivery report status to modem
    RegInfo = (If_Framework_Reg_Info_t *)Hal_Mem_Alloc(sizeof(If_Framework_Reg_Info_t));
    Hal_Mem_Set(RegInfo, 0, sizeof(If_Framework_Reg_Info_t));
    RegInfo->AppFunc    = Sms_Main;
    RegInfo->NameTextId = PHONE_TEXT_ID_SMS;
    RegInfo->AppImage   = IMG_MENU_SMS;
    Framework_App_Register_Req(RegInfo);
}

/**
 * entry of message APP
 * param (in) void
 * return  void
 */
VOID Sms_Main(VOID)
{
    Nav_Func_List_t            FuncList;
    UI_Normal_List_Only_Desc_t *pMessage   = NULL;
    UI_Button_Desc_t           *ButtonList = NULL;

    pMessage = (UI_Normal_List_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_List_Only_Desc_t));
    Hal_Mem_Set(pMessage, 0, sizeof(UI_Normal_List_Only_Desc_t));

    ButtonList = (UI_Button_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Button_Desc_t) * 9);
    Hal_Mem_Set(ButtonList, 0, sizeof(UI_Button_Desc_t) * 9);

    g_MsgGlobalCtrl.pChart = pMessage;

    pMessage->TitleBar.TxtId      = PHONE_TEXT_ID_SMS;
    pMessage->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;
    pMessage->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_MID;

    pMessage->List.ListButtonCnt = 9;
    pMessage->List.Index         = 0;
    pMessage->List.ButtonList    = ButtonList;

    ButtonList[0].ButtonIndex = 0;
    ButtonList[0].TxtId       = PHONE_TEXT_ID_CREATE_SMS;
    ButtonList[0].ReleaseFun  = Sms_Create;
    ButtonList[0].SymbolId1   = SYMBOL_SENTSMS;
    ButtonList[0].Valid       = true;
    ButtonList[1].ButtonIndex = 1;
    ButtonList[1].TxtId       = PHONE_TEXT_ID_INBOX;
    ButtonList[1].ReleaseFun  = Sms_Inbox;
    ButtonList[1].SymbolId1   = SYMBOL_INBOX;
    ButtonList[1].Valid       = true;
    ButtonList[2].ButtonIndex = 2;
    ButtonList[2].TxtId       = PHONE_TEXT_ID_SENT_ITEMS;
    ButtonList[2].ReleaseFun  = Sms_Sent_Items;
    ButtonList[2].SymbolId1   = SYMBOL_SENT_ITEM;
    ButtonList[2].Valid       = true;
    ButtonList[3].ButtonIndex = 3;
    ButtonList[3].TxtId       = PHONE_TEXT_ID_DRAFTS;
    ButtonList[3].ReleaseFun  = Sms_Drafts;
    ButtonList[3].SymbolId1   = SYMBOL_DRAFT;
    ButtonList[3].Valid       = true;
    ButtonList[4].ButtonIndex = 4;
    ButtonList[4].TxtId       = PHONE_TEXT_ID_DISTRIBUTION_LISTS;
    ButtonList[4].ReleaseFun  = Sms_Distribution_Lists;
    ButtonList[4].SymbolId1   = SYMBOL_DISTR;
    ButtonList[4].Valid       = true;
    ButtonList[5].ButtonIndex = 5;
    ButtonList[5].TxtId       = PHONE_TEXT_ID_SCREENED_SMS;
    ButtonList[5].ReleaseFun  = Sms_Screened_Msgs;
    ButtonList[5].SymbolId1   = SYMBOL_SCREENED;
    ButtonList[5].Valid       = true;
    ButtonList[6].ButtonIndex = 6;
    ButtonList[6].TxtId       = PHONE_TEXT_ID_DEL_SMS;
    ButtonList[6].ReleaseFun  = Sms_Delete;
    ButtonList[6].SymbolId1   = LV_SYMBOL_TRASH;
    ButtonList[6].Valid       = true;
    ButtonList[7].ButtonIndex = 7;
    ButtonList[7].TxtId       = PHONE_TEXT_ID_SMS_SET;
    ButtonList[7].ReleaseFun  = Sms_Setting;
    ButtonList[7].SymbolId1   = LV_SYMBOL_SETTINGS;
    ButtonList[7].Valid       = true;
    ButtonList[8].ButtonIndex = 8;
    ButtonList[8].TxtId       = PHONE_TEXT_ID_SERV_COM_EDITOR;
    ButtonList[8].ReleaseFun  = Sms_Ussd;
    ButtonList[8].SymbolId1   = SYMBOL_SERVICE;
    ButtonList[8].Valid       = true;

    pMessage->BtnBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;
    pMessage->BtnBar.ButtonR.Valid = true;

    pMessage->BtnBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;
    pMessage->BtnBar.ButtonM.Valid = true;

    pMessage->KeyFunc = Sms_Main_Handle;

    FuncList.OnShow         = (Void_P_Func_t)UI_Normal_List_Only_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = Sms_Common_Create;
    FuncList.OnDestroy      = Sms_List_Only_Common_Destory; // temp
    FuncList.OnSaveState    = Sms_Common_Save;              // temp
    FuncList.OnRestoreState = Sms_Common_Restore;           // temp

    // APP UI entry
    Nav_Forward(ACT_ID_ANY, ACT_ID_MESSAGES, &FuncList, (void *)pMessage);
} /* Sms_Main */

/**
 * entry of message APP
 * param (in) void
 * return  void
 */
UINT8 Sms_Main_Handle(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Result = 0;

    // handle the return key
    if (KEY_RELEASED == key_sta)
    {
        if (KEY_SOFTRIGHT == key_val)
        {
            Nav_Back(ACT_ID_ANY);
            Result = 1;
        }
        else if (KEY_END == key_val)
        {
            Nav_Home(ACT_ID_ANY);
            Result = 1;
        }
    }

    return(Result);   // temp
}
